package com.authine.cloudpivot.app.openapi.dto.domainmodel;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;
import java.util.*;

/**
 * @author carter
 * create_date  2020/6/4 15:19
 * description     路由定义值对象
 */
@ApiModel("路由定义值对象")
@Data
public class GatewayRouteDefinition implements Serializable {

    private static final long serialVersionUID = 1000L;

    @ApiModelProperty("路由的id")
    private String id;

    @ApiModelProperty("跳转的uri")
    private String uri;

    @ApiModelProperty("顺序")
    private Integer order;

    @ApiModelProperty("过滤器列表")
    private List<GatewayFilterDefinition> filters;

    @ApiModelProperty("断言列表")
    private List<GatewayPredicateDefinition> predicates;

    @ApiModelProperty("元数据")
    private Map<String, Object> metadata;

    @ApiModelProperty("路由备注")
    private String memo;

    @Data
    @Builder
    @NoArgsConstructor
    @AllArgsConstructor
    @ApiModel("过滤器值对象")
    public static class GatewayFilterDefinition {

        @ApiModelProperty("过滤器名称")
        private String name;

        @ApiModelProperty("过滤器的参数")
        private Map<String, String> args = new LinkedHashMap<>();
    }

    @Data
    @Builder
    @NoArgsConstructor
    @AllArgsConstructor
    @ApiModel("断言值对象")
    public static class GatewayPredicateDefinition {
        @ApiModelProperty("断言的名称")
        private String name;

        @ApiModelProperty("断言参数")
        private Map<String, String> args = new LinkedHashMap<>();
    }

    @Data
    @Builder
    @NoArgsConstructor
    @AllArgsConstructor
    @ApiModel("过滤器参数值对象")
    public static class FilterArgs {

        @ApiModelProperty("正则表达式")
        private String regexp;

        @ApiModelProperty("替换")
        private String replacement;
    }

    @Data
    @Builder
    @NoArgsConstructor
    @AllArgsConstructor
    @ApiModel("断言参数值对象")
    public static class PredicateArgs {

        @ApiModelProperty("正则表达式")
        private String pattern;

    }

    public static void main(String[] args) {

        GatewayRouteDefinition gatewayRouteDefinition = new GatewayRouteDefinition();

        gatewayRouteDefinition.setId("openapi-route");
        gatewayRouteDefinition.setMemo("openapi路由规则");
        gatewayRouteDefinition.setOrder(1);
        gatewayRouteDefinition.setUri("lb://app-org");

//        '[{\"name\":\"StripPrefix\",\"args\":{\"parts\":\"2\"}}]'
        Map<String, String> argsMap = new HashMap<>();
        argsMap.put("parts", "2");
        gatewayRouteDefinition.setFilters(Arrays.asList(
                GatewayFilterDefinition.builder().name("openApiFilter").build(),
                GatewayFilterDefinition.builder().name("StripPrefix").args(argsMap).build()
        ));

        //'[{\"name\":\"Path\",\"args\":{\"pattern\":\"/api/lifecycle/**\"}}]',
        Map<String, String> map = new HashMap<>();
        map.put("pattern", "/api/openapi/org/**");
        gatewayRouteDefinition.setPredicates(Arrays.asList(GatewayPredicateDefinition.builder().name("Path").args(map).build()));


    }
}
